package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.mapper.OrdersMapper;
import com.travel.mapper.PurchaseMapper;
import com.travel.pojo.Order;
import com.travel.pojo.Product;
import com.travel.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl extends ServiceImpl<OrdersMapper, Order> implements OrderService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Override
    public List<Map<String, Object>> ordersByUserId(int userId,int status) {
        List<Map<String, Object>> maps = new ArrayList<>();
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<Order>().eq("user_id", userId).eq("status", status);
        List<Order> orderList = ordersMapper.selectList(orderQueryWrapper);
        for (Order order : orderList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", order.getId());
            map.put("orderTime", order.getTime());
            map.put("orderStatus", order.getStatus());
            QueryWrapper<Product> productQueryWrapper = new QueryWrapper<Product>().eq("id", order.getProductId());
            Product product = purchaseMapper.selectOne(productQueryWrapper);
            if (product != null) {
                map.put("orderName", product.getName());
                map.put("orderImage", product.getPathImg());
                maps.add(map);
            }
        }
        return maps;
    }
}
